/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.ScreenEngine;
import core.Settings;
import view.additional.CellItem;
import view.additional.MenuList;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class FromDispTimeSelectForm extends BasicForm implements MenuList.MenuListSelection
{
    private static final int SAVE_COMMAND_ID = 1;
    
    private MenuList items;
    
    public FromDispTimeSelectForm()
    {
        super("from_disp");
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setScrollable(false);
        
        String min = (new Label("min")).getText();
        items = new MenuList();
        items.addItem(new CellItem(5 ,"5 " + min));
        items.addItem(new CellItem(10 ,"10 " + min));
        items.addItem(new CellItem(15 ,"15 " + min));;
        items.setSelectionListener(this);
        addComponent(items);
        
        //Команды
        addCommand(new BackCommand());
        
        Command save = new Command("save", SAVE_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                itemSelected(items.getSelectedId());
                
                evt.consume();
            }
        };
        addCommand(save);
    }
    
    public void show()
    {
        //Устанавливаем активный пункт меню
        setSelectedItem();
        
        super.show();
    }
    
    private void setSelectedItem()
    {
        int time = Settings.getSettings().getDispTime();
        for (int i = 0; i < items.size(); ++i)
        {
            if (((CellItem)items.getModel().getItemAt(i)).getId() == time)
            {
                items.setSelectedIndex(i);
                break;
            }
        }
    }
    
    public void itemSelected(int id)
    {
        if (id > 0)
        {
            //Сохраняем
            Settings.getSettings().setDispTime(id);
            Settings.getSettings().save();
            
            //Выходим в надменю
            ScreenEngine.getEngine().pop(true);
        }
    }
}
